<template>
  <div class="word-viewer">
    <div class="header">
      <h1>英语单词学习</h1>
      <div class="progress">
        <span>{{ currentIndex + 1 }} / {{ words.length }}</span>
      </div>
    </div>

    <div class="word-card" v-if="currentWord">
      <div class="word-header">
        <h2 class="word">{{ currentWord.word }}</h2>
        <span class="frequency" :class="currentWord.frequency">{{ currentWord.frequency || '中频' }}</span>
      </div>

      <div class="phonetic">
        <div class="american">
          <span class="label">美式:</span>
          <span class="symbol">{{ currentWord.american_phonetic }}</span>
        </div>
        <div class="british">
          <span class="label">英式:</span>
          <span class="symbol">{{ currentWord.british_phonetic }}</span>
        </div>
      </div>

      <div class="examples">
        <div class="example">
          <div class="chinese">{{ currentWord.chinese_example }}</div>
          <div class="english">{{ currentWord.english_example }}</div>
        </div>
      </div>

      <div class="time">添加时间: {{ currentWord.time }}</div>
    </div>

    <div class="navigation">
      <button @click="prevWord" :disabled="currentIndex === 0">上一个</button>
      <button @click="nextWord" :disabled="currentIndex === words.length - 1">下一个</button>
    </div>

    <div class="filter">
      <select v-model="selectedFrequency">
        <option value="all">全部</option>
        <option value="高频">高频</option>
        <option value="中频">中频</option>
        <option value="低频">低频</option>
      </select>
    </div>
  </div>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import wordData from '../../json/en.json'

export default {
  name: 'WordViewer',
  setup() {
    const words = ref(wordData.words || [])
    const currentIndex = ref(0)
    const selectedFrequency = ref('all')

    const filteredWords = computed(() => {
      if (selectedFrequency.value === 'all') {
        return words.value
      }
      return words.value.filter((word) => word.frequency === selectedFrequency.value)
    })

    const currentWord = computed(() => {
      return filteredWords.value[currentIndex.value]
    })

    const nextWord = () => {
      if (currentIndex.value < filteredWords.value.length - 1) {
        currentIndex.value++
      }
    }

    const prevWord = () => {
      if (currentIndex.value > 0) {
        currentIndex.value--
      }
    }

    // 当筛选条件改变时，重置当前索引
    const handleFilterChange = () => {
      currentIndex.value = 0
    }

    // 监听筛选条件变化
    onMounted(() => {
      // 添加键盘事件监听
      const handleKeyDown = (e) => {
        if (e.key === 'ArrowRight') {
          nextWord()
        } else if (e.key === 'ArrowLeft') {
          prevWord()
        }
      }

      window.addEventListener('keydown', handleKeyDown)

      // 组件卸载时移除事件监听
      return () => {
        window.removeEventListener('keydown', handleKeyDown)
      }
    })

    return {
      words,
      currentIndex,
      currentWord,
      selectedFrequency,
      filteredWords,
      nextWord,
      prevWord,
      handleFilterChange
    }
  },
  watch: {
    selectedFrequency() {
      this.currentIndex = 0
    }
  }
}
</script>

<style scoped>
.word-viewer {
  max-width: 100%;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Arial', sans-serif;
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 0 10px;
}

.header h1 {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.progress {
  font-size: 16px;
  color: #666;
}

.word-card {
  background-color: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.word-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.word {
  font-size: 32px;
  font-weight: bold;
  color: #2c3e50;
  margin: 0;
}

.frequency {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
  font-weight: bold;
}

.frequency.高频 {
  background-color: #ffebee;
  color: #c62828;
}

.frequency.中频 {
  background-color: #fff8e1;
  color: #ff8f00;
}

.frequency.低频 {
  background-color: #e8f5e9;
  color: #2e7d32;
}

.phonetic {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #eee;
}

.american,
.british {
  margin-bottom: 8px;
  font-size: 16px;
}

.label {
  font-weight: bold;
  margin-right: 8px;
  color: #555;
}

.symbol {
  color: #2c3e50;
}

.examples {
  margin-bottom: 15px;
}

.example {
  margin-bottom: 15px;
}

.chinese {
  font-size: 18px;
  margin-bottom: 8px;
  color: #333;
}

.english {
  font-size: 16px;
  color: #555;
  font-style: italic;
}

.time {
  font-size: 12px;
  color: #999;
  text-align: right;
}

.navigation {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.navigation button {
  padding: 10px 20px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.navigation button:hover {
  background-color: #3aa876;
}

.navigation button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}

.filter {
  text-align: center;
}

.filter select {
  padding: 8px 12px;
  border-radius: 6px;
  border: 1px solid #ddd;
  font-size: 16px;
  background-color: white;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .word-viewer {
    padding: 15px;
  }

  .header h1 {
    font-size: 20px;
  }

  .word {
    font-size: 28px;
  }

  .word-card {
    padding: 15px;
  }

  .navigation button {
    padding: 8px 16px;
    font-size: 14px;
  }
}

@media (max-width: 480px) {
  .word-viewer {
    padding: 10px;
  }

  .header {
    flex-direction: column;
    align-items: flex-start;
  }

  .progress {
    margin-top: 5px;
  }

  .word-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .frequency {
    margin-top: 5px;
  }

  .word {
    font-size: 24px;
  }

  .chinese {
    font-size: 16px;
  }

  .english {
    font-size: 14px;
  }
}
</style>
